//顶部导航搜索/////////
$('#search_submit').live('click', function() {
	var keywords = $("#keywords").val();
	var search_category = $("#search_category").val();
	var search_type = $("#search_type").val();
	if (keywords == document.getElementById("keywords").defaultValue) {
		$('#search_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请输入关键词</span>");
		return;
	}
	
	$("#search_menu").spin();
	$.ajax({
		type: 'POST',
		url: "/index.php/frontend/search/ajax_search",
		data: {keywords: keywords, search_category: search_category, search_type: search_type},
		dataType: 'json',
		cache: false,
		success: function(data) {
			$('#search_result').empty();
			$("#search_menu").spin(false);
			switch(data)
			{
			case 'null_keywords_error':
				$('#search_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请输入关键词</span>");
				break;
			case 'max_keywords_error':
				$('#search_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请输入至多20位数字或字符</span>");
				break;
			case 'special_keywords_error':
				$('#search_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请去掉特殊字符，重新输入</span>");
				break;
			case 'null_result':
				$('#search_msg').html("<span class='tip cry'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;悲剧了，没有找到您要的查询结果</span>");
				break;
			default:
				if (keywords.length > 10) {
					temp_keywords = keywords.substring(0, 5) + "..." + keywords.substring(keywords.length - 5, keywords.length);
				} else {
					temp_keywords = keywords;
				}
				$('#search_msg').html("<strong>搜索'<span title='" + keywords + "'>" + temp_keywords + "</span>'全部结果" + data.total + "个</strong>, 用时" + data.time + "秒");
				
				for (var i = 0; i < data.details.length; i++) {
					var num = i + 1;
					if (data.details[i].type == 1) {
						if (data.details[i].category_id == 1) {
							var href = "/index.php/frontend/society/news_detail/" + data.details[i].id + "/" + data.keywords_str;
	    				} else if (data.details[i].category_id == 2) {
							var href = "/index.php/frontend/association/news_detail/" + data.details[i].id + "/" + data.keywords_str;
		    			}
						$('#search_result').append("<strong>" + num + ". <a target='_blank' href='" + href + "' title='" + data.details[i].title + "'" + ">" + data.details[i].title + "</a></strong><br />");
					} else if (data.details[i].type == 2) {
						if (data.details[i].category_id == 1) {
							var href = 'ajax_get_announcement(' + data.details[i].id + ', "society");';
	    				} else if (data.details[i].category_id == 2) {
	    					var href = 'ajax_get_announcement(' + data.details[i].id + ', "association");';
		    			}
						$('#search_result').append("<strong>" + num + ". <a style='cursor: pointer;' onclick='" + href + "' title='" + data.details[i].title + "'" + ">" + data.details[i].title + "</a></strong><br />");
					}
    			}
				if (data.total > 9) {
					$('#search_result').append("<strong><a target='_blank' >查看更多</a></span>");
				}
				
				//截取字符串
				$(function() {
			    	$("#search_result strong a").each(function(){
						if ($(this).text().length > 19) {
							$(this).text($(this).text().substring(0, 19) + "...");
						}
					});
				});
			}
		}
	});
});